from tkinter import *
import os
import webbrowser
from tkinter import messagebox as msg
from tkinter.filedialog import *

root = Tk()
root.title("IDE by cold")
root.geometry("500x500")


def help():
    # msg.showinfo('Tip','xxx')
    url = r"D:\Study_Files\gitee\compilerTraining\tip\tip.html"
    webbrowser.open(url, new=0, autoraise=True)

def runhelp():
    # msg.showinfo('Tip','xxx')
    url = r"D:\Study_Files\gitee\compilerTraining\tip\tiprun.html"
    webbrowser.open(url, new=0, autoraise=True)

def openFile():
    fileName = askopenfilename()
    f = open(fileName,mode='r',encoding='utf-8')
    rows = 0
    while True:
        data = f.readline()
        if not data: break;
        code.insert(END,data)
        rows = rows +1
    print("Finished Total: {} rows".format(rows))
    f.close()

def saveFile():
    fileName = asksaveasfilename()
    content = code.get(0.0,END)#第一个参数‘0.0’是指从第0行第0列开始读取,第二个参数End表示最后一个字符
    f = open(fileName,mode='w',encoding='utf-8')
    f.writelines(content)
    f.close()

def clearCode():
    code.delete(0.0, END)
    res.delete(0.0,END)

def compileOption():
    #默认打开test.c
    # fileName = askopenfilename()
    fileName = "test.c"
    # cifa
    os.system("cd  ../linux/cifa  &&  flex  scanner.l")
    os.system("cd  ../linux/cifa  &&  gcc lex.yy.c   -o  scanner")
    os.system(r"cd  ../linux/cifa && scanner.exe  < {} > cifa.out".format(r'D:\Study_Files\gitee\compilerTraining\ui\test.c'))
    #other 生成在ui当前路径
    # 不用每次执行
    # os.system("flex    ../linux/tinyc/scanner.l")
    # os.system(" bison -vdty   ../linux/tinyc/parser.y")
    # os.system("gcc lex.yy.c   y.tab.c   -o  tcc-frontend")
    os.system(r" tcc-frontend.exe   {} ".format(fileName))#不要用 < 、> 强行限制他的输入输出

def resCifa():
    res.delete(0.0, END)
    f = open(r'../linux/cifa/cifa.out',mode='r',encoding='utf-8')
    rows = 0
    while True:
        data = f.readline()
        if not data: break;
        res.insert(END, data)
        rows = rows + 1
    print("Finished Total: {} rows".format(rows))
    f.close()

def resyvfa():
    #yvfa
    res.delete(0.0, END)
    f = open('y.output', mode='r', encoding='utf-8')
    rows = 0
    while True:
        data = f.readline()
        if not data: break;
        res.insert(END, data)
        rows = rows + 1
    print("Finished Total: {} rows".format(rows))
    f.close()

def resPcode():
    # pcode
    res.delete(0.0, END)
    f = open('test.asm', mode='r', encoding='utf-8')
    rows = 0
    while True:
        data = f.readline()
        if not data: break;
        res.insert(END, data)
        rows = rows + 1
    print("Finished Total: {} rows".format(rows))
    f.close()

def resDcode():
    res.delete(0.0, END)
    f = open('test.inc', mode='r', encoding='utf-8')
    rows = 0
    while True:
        data = f.readline()
        if not data: break;
        res.insert(END, data)
        rows = rows + 1
    print("Finished Total: {} rows".format(rows))
    f.close()

def bye():
    #
    root.destroy()

#cai dan
menuBar = Menu(master=root)
root.configure(menu =menuBar)
#
fileMenu = Menu(tearoff=0)
fileMenu.add_command(label="Open",command=openFile)
fileMenu.add_command(label="Save",command=saveFile)
fileMenu.add_command(label="Exit",command=bye)
#
languageMenu = Menu(tearoff=0)
languageMenu.add_command(label="TinyC")
#
compileMenu = Menu(tearoff=0)
compileMenu.add_command(label="一键编译",command=compileOption)
compileMenu.add_command(label="词法分析",command=resCifa)
compileMenu.add_command(label="语法分析",command=resyvfa)
# compileMenu.add_command(label="语义分析")
compileMenu.add_command(label="中间代码生成",command=resPcode)
compileMenu.add_command(label="目标代码生成",command=resDcode)
#
menuBar.add_cascade(label="File",menu=fileMenu)
menuBar.add_cascade(label="Language",menu=languageMenu)
menuBar.add_cascade(label="Run",command=runhelp)
menuBar.add_cascade(label="Compile",menu=compileMenu)
menuBar.add_cascade(label="Clear",command=clearCode)
menuBar.add_cascade(label="Help",command=help)
#
# label =Label(root,text='1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n')
# label.pack(fill=Y,side=LEFT)
code = Text(height=18)
code.pack()
res = Text()
res.config()
# res.config(state=DISABLED)
res.pack()



#man you
root.mainloop()









root.mainloop()